<?php

class Admin_CourseMediaController extends Admin_Controller_Base {

   public function indexAction() {
      
      $db = Zend_Registry::get('db');
      $select = $db->select()
                   ->from(array('cm' => 'coursemedia'))
                   ->join(array('c' => 'courses'), 'cm.courseid = c.id', array('courseid' => 'id', 'title', 'lang'))
                   ->group(array('cm.id'))
                   
      ;
      $stmt = $db->query($select);
      $courseMedia = $stmt->fetchAll();
      $this->view->courseMedia = $courseMedia;
      $this->_setTitle('CourseMedia - List');
      $this->_setPaginator('courseMedia', $select);
   }
   
   public function importAction() {
      $this->_setTitle('CourseMedia - Import');
      $form = new Admin_Form_CourseMedia_Import(array('params' => $this->getRequest()->getParams()));
      $this->view->form = $form;
      if($this->getRequest()->isPost()) {
         if($form->isValid($this->_request->getPost())) {
            $courseid = $form->course->getValue();
            $folder = $form->folder->getValue();
            $ext = $form->ext->getValue();
            $mime = $form->mime->getValue();
            $type = $form->type->getValue();
            $absfolder = Zend_Registry::get('absfolder');
            Zend_Registry::set('absfolder', null);
            
            $courseModel = new Model_Courses();
            $uid = $courseModel->findOneBy('courseid', $courseid);
            
            $dir = new DirectoryIterator($absfolder);
            $files = array();
            foreach($dir as $fileInfo) {
               if(!$fileInfo->isDot()) {
                  if(endsWith($fileInfo->getPathName(), '.'.$ext)) {
                     $info = pathinfo($fileInfo->getPathName());
                     $info['uid'] = md5(uniqid(mt_rand(), true));
                     $info['course_uid'] = $uid->uid;
                     $files[$info['filename']] = $info;
                  }
               }
            }
            sort($files);
            $db = Zend_Registry::get('db');
            // target folder
            $targetfolder = sprintf('%s/%s', dirname($absfolder), $uid->uid);
            if(!file_exists($targetfolder)) {
               mkdir($targetfolder);
            }
            $select = $db->select()
                         ->from('coursemedia', array('count' => new Zend_Db_Expr('MAX(mediaid)')))
                         ->where($db->quoteInto('courseid = ?', $courseid, 'INTEGER'))
            ;                                     
            $stmt = $db->query($select);
            $countObj = $stmt->fetch();
            
            $count = $countObj['count'];
            foreach($files as $fname => $file) {
               $from = sprintf("%s/%s", $absfolder, $file['basename']);
               $to = sprintf("%s/%s.%s", $targetfolder, $file['uid'], $ext);
               copy($from, $to);
               $data = array(
                  'courseid' => $courseid,
                  'mediaid' => ++$count,
                  'uid' => $file['uid'],
                  'ext' => $ext,
                  'type' => $type,
                  'mime' => $mime,
                  'duration' => 0,
                  'description' => ''
               );
               echo sprintf('Added : [%s] %s<br />', $count, $file['basename']);
               $db->insert('coursemedia', $data);
            }
            //$this->_redirect("/admin/coursemedia");
         }

      }
   }
   
   public function addAction() {
      $this->_setTitle('CourseMedia - Add');
      $form = new Admin_Form_Users(array('params' => $this->getRequest()->getParams()));
      $this->view->form = $form;
      
      if($this->getRequest()->isPost()) {
         if($form->isValid($this->_request->getPost())) {
            $user = new Model_User();
            foreach($user->getFields() as $field) {
               $value = trim($form->$field->getValue());
               if($value) {
                  $user->$field = $value;
               }
            }
            $user->id = null;
            $user->insert();
            $this->_redirect("/admin/users");
         }

      }
   }
   
   public function editAction() {
      $this->_setTitle('Users - Edit');
      $form = new Admin_Form_Users(array('params' => $this->getRequest()->getParams()));
      $this->view->form = $form;
      
      if($this->getRequest()->isPost()) {
         if($form->isValid($this->_request->getPost())) {
            $user = new Model_User();
            foreach($user->getFields() as $field) {
               $value = trim($form->$field->getValue());
               if($value) {
                  $user->$field = $value;
               }
            }
            $user->update();
            $this->_redirect("/admin/users");
         }

      }
   }
   
   public function deleteAction() {
      $this->_setTitle('Users - Delete');
      $params = $this->getRequest()->getParams();
      $user = new Model_User();
      $user->id = $params['id'];
      $user->delete();
      $this->_redirect("/admin/users");
   }

   

}
